package burubaka.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.arnx.jsonic.JSON;

import org.slim3.datastore.Datastore;

import burubaka.common.BurubakaUtil;
import burubaka.meta.NominationHorseMeta;
import burubaka.model.NominationHorse;
import burubaka.model.RaceInfo;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;

public class NominationHorseService {

    private static final Logger log = Logger
        .getLogger(NominationHorseService.class.getName());
    private NominationHorseMeta meta = new NominationHorseMeta();

    public List<NominationHorse> findByRaceInfoKey(Key key) {
        if (key == null) {
            return new ArrayList<NominationHorse>();
        }
        return Datastore.query(meta, key).asList();
    }

    public boolean regist(Map<String, Object> input, RaceInfo raceInfo) {
        log.info(input.toString());
        NominationHorse[] nh =
            JSON.decode(
                (String) input.get("nomiHorseL"),
                NominationHorse[].class);

        // Map<String, Object> map =
        // JSON.decode((String)input.get("json"),HashMap.class);
        // List<String> horseNames = BurubakaUtil.getRequestArray(map,
        // "horseName");
        // List<String> weight = BurubakaUtil.getRequestArray(map, "weight");

        Transaction tx = Datastore.beginTransaction();

        List<Key> old = gatKeyList(raceInfo.getKey());
        Datastore.delete(old);

        // List<NominationHorse> list =
        // NominationHorseService.getNominationHorseList(horseNames,null,weight,raceInfo.getKey());

        List<NominationHorse> list =
            NominationHorseService
                .getNominationHorseList(nh, raceInfo.getKey());
        Datastore.put(list);
        tx.commit();
        return true;
    }

    /**
     * 一覧登録
     * 
     * @param list
     * @param raceInfo
     * @return true:成功
     */
    public boolean regist(List<NominationHorse> list, RaceInfo raceInfo) {
        Transaction tx = Datastore.beginTransaction();
        Key key = raceInfo.getKey();
        List<Key> old = gatKeyList(key);
        Datastore.delete(old);
        for (NominationHorse nh : list) {
            if (key != null) {
                Key childKey = Datastore.allocateId(key, NominationHorse.class);
                nh.setKey(childKey);
            }
        }
        Datastore.put(list);
        tx.commit();
        return true;
    }

    /**
     * @param hoseNames
     * @param hoseNamesA
     * @param weight
     * @param key
     * @return NominationHorseList
     */
    public static List<NominationHorse> getNominationHorseList(
            List<String> hoseNames, List<String> hoseNamesA,
            List<String> weight, Key key) {

        List<NominationHorse> list = new ArrayList<NominationHorse>();
        for (int i = 0; i < hoseNames.size(); i++) {
            NominationHorse nh = new NominationHorse();
            nh.setHorseName(hoseNames.get(i));
            if (BurubakaUtil.isNotEmpty(hoseNamesA, i)) {
                nh.setHoseNamesA(hoseNamesA.get(i));
            }
            if (BurubakaUtil.isNotEmpty(weight, i)) {
                nh.setWeight(weight.get(i));
            }
            if (key != null) {
                Key childKey = Datastore.allocateId(key, NominationHorse.class);
                nh.setKey(childKey);
            }
            list.add(nh);
        }
        return list;
    }

    public static List<NominationHorse> getNominationHorseList(
            NominationHorse[] nomiHorses, Key key) {
        List<NominationHorse> list = new ArrayList<NominationHorse>();
        for (NominationHorse nh : nomiHorses) {
            if (key != null) {
                Key childKey = Datastore.allocateId(key, NominationHorse.class);
                nh.setKey(childKey);
            }
            list.add(nh);
        }
        return list;
    }

    public List<NominationHorse> getNominationHorseList(RaceInfo raceInfo) {
        return this.findByRaceInfoKey(raceInfo.getKey());
    }

    public List<NominationHorse> getNominationHorseList(RaceInfo raceInfo,
            String text) {
        List<NominationHorse> list = null;
        if (raceInfo != null) {
            list = this.findByRaceInfoKey(raceInfo.getKey());
        }

        if (list == null || list.isEmpty()) {
            String dataTbl =
                BurubakaUtil.substring(
                    "<div class=\"dataTbl\">",
                    "</div>",
                    text);

            ArrayList<String> hoseNamesA =
                BurubakaUtil.substringList(
                    "<td class=\"bamei\">",
                    "</td>",
                    dataTbl);
            log.info(hoseNamesA.toString());

            ArrayList<String> hoseNames =
                BurubakaUtil.substringList(
                    "'>",
                    "</a>",
                    hoseNamesA.toString());
            log.info(hoseNames.toString());

            ArrayList<String> weight =
                BurubakaUtil.substringList(
                    "<td class=\"kinryou\">",
                    "</td>",
                    dataTbl);
            log.info(weight.toString());

            list =
                NominationHorseService.getNominationHorseList(
                    hoseNames,
                    hoseNamesA,
                    weight,
                    null);
        }
        return list;
    }

    /**
     * 全削除
     * 
     * @param raceInfo
     * @return true:成功
     */
    public boolean deleteAll(RaceInfo raceInfo) {
        Transaction tx = Datastore.beginTransaction();
        Datastore.delete(gatKeyList(raceInfo.getKey()));
        tx.commit();
        return true;
    }

    private List<Key> gatKeyList(Key key) {
        if (key == null) {
            return new ArrayList<Key>();
        }
        return Datastore.query(meta, key).asKeyList();
    }

}
